/**
 * 下拉框分层级组件
 */

(function( $ ){
	
	  $.fn.jCreateOption = function(rowData,setting) {
		  var opts = $.extend({},$.fn.jCreateOption.defaults,setting);
	  	
		  this.empty();//清空下级下拉框 
		  if(opts.addEmptyOption)
		  {
			  $("<option value='"+opts.emptyVal+"'>"+opts.emptyText+"</option>").appendTo(this);
			  
		  }
		  createOption(opts,this, rowData, 0);
		  
	  };
	  $.fn.jCreateOption.defaults = {    
			  id: 'id',    
			  name: 'name',
			  children: 'lstSubHierarchyVO',
			  selVal : '',
			  addEmptyOption:true,
			  emptyVal:'',
			  emptyText:'请选择...'
	  }; 
	  function createOption(opts,obj, rowData, level)
	  {
		  var spaceStr = "";
		  for(i=0; i< level; i++)
	  	  {
			  spaceStr = "&nbsp;&nbsp;&nbsp;"+spaceStr;
	  	  }
	  	  $.each(rowData, function(i, item) {
	  		  if(opts.selVal ==  item.pTableVO[opts.id])
	  		  {
	  			$("<option selected='selected' value='" + item.pTableVO[opts.id] + "'>" + spaceStr+item.pTableVO[opts.name] + "</option>").appendTo(obj);//动态添加Option子项
	  		  }else
	  		  {
	  			$("<option value='" + item.pTableVO[opts.id] + "'>" + spaceStr+item.pTableVO[opts.name] + "</option>").appendTo(obj);
	  		   }
	  		if(item.lstSubHierarchyVO != undefined)
	  		{
	  			createOption(opts,obj,item[opts.children], ++level);
	  			
	  		}
	  	});
	  }
	})( jQuery );